package UnionSearchSet;

import java.io.BufferedInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;

/**
 * @Author Fizz Pu
 * @Date 2021/4/26 下午5:04
 * @Version 1.0
 * 失之毫厘，缪之千里！
 */

import java.util.*;

public class uoj127 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int testCount = scanner.nextInt();

        for(int i = 0; i < testCount; i++){
            int k = scanner.nextInt();
            UnionSearchSet us = new UnionSearchSet(k);
            List<List<Integer>> unequals = new ArrayList<>();
            int flag = 0;

            for(int j = 0; j < k; j++){
                int x = scanner.nextInt() - 1;
                int y = scanner.nextInt() - 1;
                int e = scanner.nextInt();

                if(e == 1){
                    // 进行合并
                    us.union(x, y);
                } else {
                    // 暂时吧不等于的存储下来
                    // unequals.add(List.of(x, y));
                    ArrayList<Integer> inner = new ArrayList<>();
                    inner.add(x);
                    inner.add(y);
                    unequals.add(inner);
                }
            }

            for(List<Integer> list : unequals){
                int x = list.get(0);
                int y = list.get(1);
                int rootX = us.find(x);
                int rootY = us.find(y);
                if(rootX == rootY){
                    flag = 1;
                }
            }

            // 结果输出
            if(flag == 1){
                System.out.println("NO");
            } else {
                System.out.println("YES");
            }
        }

    }
}
